package com.sendi.mobilework.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.sendi.mobilework.util.Utils;

/**
 * 自定义侧滑选择
 * Created by Administrator on 2017/4/17.
 */

public class SwipeLayout extends FrameLayout {

    private ViewDragHelper mDragHelper;
    private View mBackView;
    private View mForntView;
    private int mHeight;
    private int mWidth;
    private int mRange;
    private OnSwipeLayoutListener mOnSwipeLayoutListener;
    private Status mStatu=Status.Close;//默认关闭状态

    public static enum Status{//状态
        Close,Open,Draging;
    }

    public void setOnSwipeLayoutListener(OnSwipeLayoutListener onSwipeLayoutListener) {
        mOnSwipeLayoutListener = onSwipeLayoutListener;
    }

    public static interface OnSwipeLayoutListener{
        void onClose(SwipeLayout swipeLayout);
        void onOpen(SwipeLayout swipeLayout);
        void onDraging(SwipeLayout swipeLayout);
        void onStartClose(SwipeLayout swipeLayout);
        void onStartOpen(SwipeLayout swipeLayout);
    }

    public SwipeLayout(Context context) {
        this(context,null);
    }

    public SwipeLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mDragHelper=ViewDragHelper.create(this,1.0f,mCallback);
    }

    ViewDragHelper.Callback mCallback=new ViewDragHelper.Callback() {
        //重写监听
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            //返回true为可以拖拽
            return true;
        }

        //限定移动范围

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (child==mForntView){
                if (left>mWidth){
                    return 0;
                }else if (left<-mRange){
                    return -mRange;
                }
            }else if (child==mBackView){
                if (left<mWidth-mRange){
                    return mWidth-mRange;
                }
            }
            
            return left;
        }
        
        //设置侧滑的开关
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (xvel==0&&releasedChild.getLeft()<-mRange/2){
                //打开
                open();
            }else if (xvel<0){
                //打开
                open();
            }else {
                //关闭
                close();
            }
        }

        //传递事件
        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            super.onViewPositionChanged(changedView, left, top, dx, dy);
            if (changedView==mForntView){
                mBackView.offsetLeftAndRight(dx);
            }else if (changedView==mBackView){
                mForntView.offsetLeftAndRight(dx);
            }

            dispatchSwipeEvent();

            //兼容老版本
            invalidate();
        }
    };

    private void dispatchSwipeEvent() {
        if (mOnSwipeLayoutListener!=null){
            mOnSwipeLayoutListener.onDraging(this);
        }

        Status finalStatu=mStatu;//记录最后的状态
        //获取当前状态
        mStatu=updateStatus();
        //状态改变，方法随着调用
        if (mStatu!=finalStatu){

                if (mOnSwipeLayoutListener!=null){
                if (mStatu==Status.Close){
                    mOnSwipeLayoutListener.onClose(this);
                }else if (mStatu==Status.Open){
                    mOnSwipeLayoutListener.onOpen(this);
                }else if (mStatu==Status.Draging){
                    if (finalStatu==Status.Close){
                        mOnSwipeLayoutListener.onStartOpen(this);
                    }else if (finalStatu==Status.Open){
                        mOnSwipeLayoutListener.onStartClose(this);
                    }
                }
            }

        }
    }

    private Status updateStatus() {
        int left=mForntView.getLeft();
        if (0==left){
            //处于关闭状态
            return Status.Close;
        }else if (-mRange==left){
            //处于打开状态
            return Status.Open;
        }
        return Status.Draging;
    }

    private void close() {
        Utils.showToast(getContext(),"close");
        close(true);
    }

   public void close(boolean isSmooth) {
        int finalLeft=0;
       if (isSmooth){
           if (mDragHelper.smoothSlideViewTo(mForntView,finalLeft,0)){
               ViewCompat.postInvalidateOnAnimation(this);
           }
       }else {
           layoutContent(false);
       }
    }

    private void open() {
        Utils.showToast(getContext(),"open");
        open(true);
    }

    public void open(boolean isSmooth) {
        int finalLeft=-mRange;

        if (isSmooth){
            //开始动画
            if (mDragHelper.smoothSlideViewTo(mForntView,finalLeft,0)){
                ViewCompat.postInvalidateOnAnimation(this);
            }
        }else {
            layoutContent(true);
        }

    }

    //滑动时候执行
    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mDragHelper.continueSettling(true)){
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    private void layoutContent(boolean isOpen) {
        //摆放FrontView
        Rect frontRect=computeFrontViewRect(isOpen);
        mForntView.layout(frontRect.left,frontRect.top,frontRect.right,frontRect.bottom);

        //摆放BackView
        Rect backRect=computeBackViewRect(frontRect);
        mBackView.layout(backRect.left,backRect.top,backRect.right,backRect.bottom);
    }

    private Rect computeBackViewRect(Rect frontRect) {
        int left=frontRect.right;
        return new Rect(left,0,left+mRange,mHeight);
    }

    private Rect computeFrontViewRect(boolean isOpen) {
        int left=0;
        if (isOpen){
            left=-mRange;
        }
        return new Rect(left,0,left+mWidth,mHeight);
    }

    //传递触摸事件
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        return mDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        try {
            mDragHelper.processTouchEvent(event);
        }catch (Exception e){
            e.printStackTrace();
        }

        return true;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //当XML被填充完毕时调用此方法
        mBackView=getChildAt(0);
        mForntView=getChildAt(1);
    }

    //摆放位置
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        //摆放位置
        layoutContent(false);
    }

    //测量
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mHeight=mForntView.getMeasuredHeight(); //高
        mWidth=mForntView.getMeasuredWidth();   //长
        mRange=mBackView.getMeasuredWidth();    //滑动范围
    }
}
